package com.sample.thread.bizhall;

import java.util.concurrent.CountDownLatch;

/**
* @comments 营业大厅
* “synchronized”关键字的使用
* @author  zg
* @date 2021年3月1日
*/
public class BusinessHall {
    
    //（所有窗口当天）最多处理业务数
    private static final int TRANS_MAX = 20;
    //序号
    private static int seqNum = 1;
    
    /**
     * 获取序号牌
     * @return
     */
    public synchronized static Integer getSeqNum() {
        if(seqNum > TRANS_MAX) {
            return null;
        }
        return seqNum ++;
    }
    

    public static void main(String[] args) throws InterruptedException {
        long timeStart = System.currentTimeMillis();
        CountDownLatch counter = new CountDownLatch(3);
        //创建若干个“业务窗口” ------------------------------------------
        
        // ------------ 继承 Thread 类 -------------------
        BusinessWindowThread win1 = new BusinessWindowThread(1, counter);
        BusinessWindowThread win2 = new BusinessWindowThread(2, counter);
        BusinessWindowThread win3 = new BusinessWindowThread(3, counter);
        win1.start();
        win2.start();
        win3.start();
        
        // ------------ 实现继承 Runnable 接口（营业厅开放3个业务窗口） -------------------
        /*BusinessWindowRunnable winRunnable = new BusinessWindowRunnable();
        Thread t1 = new Thread(winRunnable, "1");//窗口1
        Thread t2 = new Thread(winRunnable, "2");//窗口2
        Thread t3 = new Thread(winRunnable, "3");//窗口3
        t1.start();
        t2.start();
        t3.start();*/

        counter.await();//等待所有线程执行完毕
        System.out.println("今天任务完成，下班！用时" + (System.currentTimeMillis() - timeStart)/1000.0 + "s");
    }

}
